package _SED.client;

import gwtupload.client.IUploadStatus.Status;
import gwtupload.client.IUploader;
import gwtupload.client.IUploader.UploadedInfo;
import gwtupload.client.MultiUploader;
import gwtupload.client.PreloadedImage;

import java.beans.XMLEncoder;
import java.io.ByteArrayOutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.io.*;

//import com.smartgwt.client.widgets.IButton;
//import com.smartgwt.client.widgets.Window;
//import com.smartgwt.client.widgets.events.ClickEvent;
//import com.smartgwt.client.widgets.events.ClickHandler;
//import com.smartgwt.client.widgets.events.CloseClickHandler;

import _SED.shared.FieldVerifier;

//import com.google.appengine.api.datastore.Entity;

import com.google.gwt.cell.client.DateCell;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.ColumnSortEvent;
import com.google.gwt.user.cellview.client.ColumnSortEvent.AsyncHandler;
import com.google.gwt.user.cellview.client.SimplePager;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ChangeListener;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.DockPanel.DockLayoutConstant;
import com.google.gwt.user.client.ui.FileUpload;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteEvent;
import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteHandler;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HTMLTable.CellFormatter;
import com.google.gwt.user.client.ui.HTMLTable.RowFormatter;
import com.google.gwt.user.client.ui.HasAlignment;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.IntegerBox;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.LongBox;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.TabPanel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.ValueBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.datepicker.client.DateBox;
import com.google.gwt.view.client.AsyncDataProvider;
import com.google.gwt.view.client.HasData;
import com.google.gwt.view.client.SelectionChangeEvent;
import com.google.gwt.view.client.SingleSelectionModel;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
@SuppressWarnings("deprecation")
public class SCMS implements EntryPoint {
	/**
	 * The message displayed to the user when the server cannot be reached or
	 * returns an error.
	 */
	private static final String SERVER_ERROR = "An error occurred while "
			+ "attempting to contact the server. Please check your network "
			+ "connection and try again.";

	private static final DockLayoutConstant NORTH = null;

	/**
	 * Create a remote service proxy to talk to the server-side Greeting
	 * service.
	 */
	private final DBConnectionAsync dbService = GWT.create(DBConnection.class);

	/**
	 * This is the entry point method.
	 */

	private List<Rfc> RfcList = new ArrayList<Rfc>();
	private List<WO> WOList = new ArrayList<WO>();
	private User user;

	private static final String UPLOAD_ACTION_URL = GWT.getModuleBaseURL()
			+ "upload";

	public void onModuleLoad() {
		this.loadLoginView();

	}

	private void loadLoginView() {
		RootPanel.get().clear();

		final Button btnLogin = new Button("Login");

		// final Button btnCreate = new Button("Create");
		// final String lname = "Power", fname = "Max";

		final TextBox txtName = new TextBox();
		txtName.setText("User Name");
		final PasswordTextBox txtPwd = new PasswordTextBox();
		txtPwd.setText("Password");
		final Label errorLabel = new Label();

		// set size
		// btnCreate.setSize("87px", "34px");
		btnLogin.setSize("87px", "34px");

		// WUser Namee can add style names to widgets
		// btnCreate.addStyleName("Button");
		btnLogin.addStyleName("Button");

		VerticalPanel panel = new VerticalPanel();
		panel.setHeight("480px");
		panel.setVerticalAlignment(HasAlignment.ALIGN_MIDDLE);
		panel.setHorizontalAlignment(HasAlignment.ALIGN_CENTER);

		Grid grid = new Grid(4, 2);
		grid.setTitle("SCMS Login");

		grid.setText(0, 0, "Username");
		grid.setWidget(0, 1, txtName);
		grid.setText(1, 0, "Password");
		grid.setWidget(1, 1, txtPwd);
		grid.setWidget(2, 1, btnLogin);
		// grid.setWidget(3, 0, btnCreate);
		grid.setWidget(3, 1, errorLabel);
		grid.setStyleName("center");
		panel.add(grid);
		panel.add(errorLabel);
		RootPanel.get().add(panel);

		// Focus the cursor on the name field when the app loads
		txtName.setFocus(true);
		txtName.selectAll();

		// Create the popup dialog box

		// final DialogBox dialogBox = new DialogBox();
		final DialogBox dialogBox = new DialogBox();
		dialogBox.setTitle("Login Info");
		dialogBox.setAnimationEnabled(true);
		final Button closeButton = new Button("Close");
		// We can set the id of a widget by accessing its Element
		closeButton.getElement().setId("closeButton");
		final Label textToServerLabel = new Label();
		final HTML serverResponseLabel = new HTML();
		VerticalPanel dialogVPanel = new VerticalPanel();
		dialogVPanel.addStyleName("dialogVPanel");
		dialogVPanel.add(new HTML("<b>Infobox:</b>"));
		dialogVPanel.add(textToServerLabel);
		dialogVPanel.add(new HTML("<br><b>Server replies:</b>"));
		dialogVPanel.add(serverResponseLabel);
		dialogVPanel.setHorizontalAlignment(VerticalPanel.ALIGN_RIGHT);
		dialogVPanel.add(closeButton);
		dialogBox.setWidget(dialogVPanel);
		// Add a handler to close the DialogBox
		closeButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				dialogBox.hide();
				btnLogin.setEnabled(true);
				btnLogin.setFocus(true);
				;
			}
		});

		// Create a handler for the sendButton and nameField
		class MyHandler implements ClickHandler, KeyUpHandler {
			/**
			 * Fired when the user clicks on the sendButton.
			 */
			public void onClick(ClickEvent event) {

				// True if Create Button was used
				if (event.getSource() == btnLogin)
					sendNameToServer(false);
				else
					sendNameToServer(true);

			}

			/**
			 * Fired when the user types in the nameField.
			 */
			public void onKeyUp(KeyUpEvent event) {
				if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
					sendNameToServer(false);
				}
			}

			/**
			 * Send the name from the nameField to the server and wait for a
			 * response.
			 */
			private void sendNameToServer(boolean create) {
				// First, we validate the input.

				errorLabel.setText("");
				String usr = txtName.getText();
				String pwd = txtPwd.getText();
				if (!FieldVerifier.isValidName(usr)) {
					errorLabel.setText("Please enter at least four characters");
					return;
				}

				// Then, we send the input to the server.
				btnLogin.setEnabled(false);
				textToServerLabel.setText(usr);
				serverResponseLabel.setText("");

				if (create == false) {

					dbService.userAuth(usr, pwd, new AsyncCallback<String[]>() {
						public void onFailure(Throwable caught) {
							// Show the RPC error message to the user
							// dialogBox.addChild(new
							// Label().setText("Remote Procedure Call - Failure"));
							// dialogBox.setText("Remote Procedure Call - Failure");
							serverResponseLabel
									.addStyleName("serverResponseLabelError");
							serverResponseLabel.setHTML(SERVER_ERROR);
							// dialogBox.center();
							// closeButton.setFocus(true);

						}

						public void onSuccess(String[] result) {
							if (result[2] != null) {
								user = new User(result[2], result[3],
										result[4], result[5]);
								loadWelcomeView();
							} else {
								// dialogBox.setText("Login Failure");
								serverResponseLabel
										.addStyleName("serverResponseLabelError");
								serverResponseLabel.setHTML(result[1]);
								// dialogBox.center();
								// closeButton.setFocus(true);
							}
						}

					});
				} else {
//					 dbService.userCreate(usr, lname, fname, "Administrator",
//					 pwd, new
//					 AsyncCallback<String>() {
//					 public void onFailure(Throwable caught) {
//					 // Show the RPC error message to the user
//					 dialogBox
//					 .setText("Remote Procedure Call - Failure");
//					 serverResponseLabel
//					 .addStyleName("serverResponseLabelError");
//					 serverResponseLabel.setHTML(SERVER_ERROR);
//					 dialogBox.center();
//					 closeButton.setFocus(true);
//					 }
//					
//					 public void onSuccess(String result) {
//					 dialogBox.setText("Remote Procedure Call");
//					 serverResponseLabel
//					 .removeStyleName("serverResponseLabelError");
//					 serverResponseLabel.setHTML(result);
//					 dialogBox.center();
//					 closeButton.setFocus(true);
//					 }
//					 });
				}
			}
		}

		// Add a handler to send the name to the server
		MyHandler handler = new MyHandler();
		btnLogin.addClickHandler(handler);
		txtName.addKeyUpHandler(handler);
		// btnCreate.addClickHandler(handler);

	}

	@SuppressWarnings("deprecation")
	private void loadWelcomeView() {

		RootPanel.get().clear();

		// Create the popup dialog box
		final DialogBox dialogBox = new DialogBox();
		dialogBox.setText("Login successful");
		dialogBox.setAnimationEnabled(true);

		final Button closeButton = new Button("Close");
		// We can set the id of a widget by accessing its Element
		closeButton.getElement().setId("closeButton");
		final Label textToServerLabel = new Label();
		final VerticalPanel dialogVPanel = new VerticalPanel();
		dialogVPanel.addStyleName("dialogVPanel");
		dialogVPanel.add(new HTML("<b>Hello " + user.first_name + "</b>"));
		dialogVPanel.add(new HTML(
				"<br><b>Your authentification was successful!</b>"));
		dialogVPanel.setHorizontalAlignment(VerticalPanel.ALIGN_RIGHT);
		dialogVPanel.add(closeButton);
		dialogBox.setWidget(dialogVPanel);
		dialogBox.center();
		closeButton.setFocus(true);

		final HTML serverResponseLabel = new HTML();

		// Add a handler to close the DialogBox
		closeButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				dialogBox.hide();
			}
		});

		String text0 = "Welcome " + user.first_name + " " + user.last_name;
		String text1 = "Create an new Request for Change";
		String text2 = "Show the existing RFCs";
		String text3 = "Show the existing Work Orders";
		String text4 = "Administrator's Workbench";

		TabPanel panel = new TabPanel();
		DockPanel dp = new DockPanel();

		// //////// TAB 0 - Overview ---------------------------------//

		// Buttons
		final Button btnRFCExport = new Button("Export RFC");

		dp = new DockPanel();

		dp.add(new Label(text0), DockPanel.NORTH);
		dp.add(btnRFCExport, DockPanel.NORTH);
		panel.add(dp, "Home");

		// //////// TAB 1 - CREATE RFC ---------------------------------//

		VerticalPanel vp = new VerticalPanel();
		vp.setVerticalAlignment(HasAlignment.ALIGN_MIDDLE);
		vp.setHorizontalAlignment(HasAlignment.ALIGN_CENTER);

		// Buttons
		final Button btnRFCCreate = new Button("Create");
		if (user.role.equals("Change User"))
			btnRFCCreate.setEnabled(false);
		else
			btnRFCCreate.setEnabled(true);

		// TextBoxes
		final LongBox txtIDRFC = new LongBox();
		txtIDRFC.setEnabled(false);
		final TextBox txtDescRFC = new TextBox();
		txtDescRFC.setText("No ##");
		txtDescRFC.setText("RFC Description");
		final TextBox txtReasonRFC = new TextBox();
		txtReasonRFC.setText("Reasons");
		final IntegerBox txtCostsRFC = new IntegerBox();

		final TextBox txtCI = new TextBox();
		txtCI.setText(user.uname);
		txtCI.setEnabled(false);

		// TextAreas
		final TextArea txtDetailDescRFC = new TextArea();
		txtDetailDescRFC.setText("RFC Detail Information");
		final TextArea txtBenefitRFC = new TextArea();
		final TextArea txtRisksRFC = new TextArea();

		// DatePicker
		final DateBox StartRFC = new DateBox();
		StartRFC.setValue(this.getToday());
		StartRFC.getDatePicker();
		StartRFC.setTitle("valid form");

		// ListBoxes
		final ListBox lbCO = new ListBox();
		lbCO.addItem("");
		lbCO.setVisibleItemCount(1);
		final ListBox lbCA = new ListBox();
		lbCA.setVisibleItemCount(1);
		final ListBox lbPrio = new ListBox();
		lbPrio.setVisibleItemCount(1);
		lbPrio.addItem("Low");
		lbPrio.addItem("Normal");
		lbPrio.addItem("High");
		lbPrio.addItem("Citical");

		final ListBox lbService = new ListBox();
		lbService.addItem("Network");
		lbService.addItem("Client");
		lbService.addItem("Server");
		lbService.addItem("Software");
		lbService.setVisibleItemCount(1);

		final ListBox lbCI = new ListBox();
		lbCI.addItem("CI_A");
		lbCI.addItem("CI_B");
		lbCI.addItem("CI_C");
		lbCI.addItem("CI_D");

		lbCI.setVisibleItemCount(1);

		// New Grid Item to List description texts with according Widget
		Grid grid = new Grid(15, 2);
		grid.setTitle("Create RFC");
		grid.setText(0, 0, "RFC ID");
		// get next RFC ID
		dbService.getNextRfcNr(new AsyncCallback<Long>() {
			public void onFailure(Throwable caught) {
				// Show the RPC error message to the user
			}

			// Refresh User Listbox
			public void onSuccess(Long RfcNr) {
				txtIDRFC.setValue(RfcNr);
			}
		});

		grid.setWidget(0, 1, txtIDRFC);
		grid.setText(1, 0, "Summary");
		grid.setWidget(1, 1, txtDescRFC);
		grid.setText(2, 0, "Detail Description");
		grid.setWidget(2, 1, txtDetailDescRFC);
		grid.setText(3, 0, "Date of submission");
		grid.setWidget(3, 1, StartRFC);
		grid.setText(4, 0, "Change Initiator");
		grid.setWidget(4, 1, txtCI);
		grid.setText(5, 0, "Change Owner");
		grid.setWidget(5, 1, lbCO);
		grid.setText(6, 0, "Change Approver");
		grid.setWidget(6, 1, lbCA);
		grid.setText(7, 0, "Change Priority");
		grid.setWidget(7, 1, lbPrio);
		grid.setText(8, 0, "Reason for the Change");
		grid.setWidget(8, 1, txtReasonRFC);
		grid.setText(9, 0, "Costs (CHF)");
		grid.setWidget(9, 1, txtCostsRFC);
		grid.setText(10, 0, "Benefits");
		grid.setWidget(10, 1, txtBenefitRFC);
		grid.setText(11, 0, "Risks");
		grid.setWidget(11, 1, txtRisksRFC);
		grid.setText(12, 0, "Affected Services");
		grid.setWidget(12, 1, lbService);
		grid.setText(13, 0, "Affected CI's");
		grid.setWidget(13, 1, lbCI);
		grid.setStyleName("RFCCreate");
		vp.add(grid);
		vp.add(btnRFCCreate);

		final TextBox txtPwd = new TextBox();
		txtPwd.setText("Password");
		final Label errorLabel = new Label();

		dp = new DockPanel();
		dp.add(new Label(text1), DockPanel.NORTH);
		dp.add(vp, DockPanel.CENTER);

		panel.add(dp, "New RFC");

		// //////// TAB - Show Rfcs ---------------------------------//

		dp = new DockPanel();

		final CellTable<Rfc> rfcTab = new CellTable<Rfc>();
		rfcTab.setPageSize(20);

		final CellTable<WO> woTab = new CellTable<WO>();
		woTab.setPageSize(20);

		final SingleSelectionModel<Rfc> selectionModel = new SingleSelectionModel<Rfc>();
		rfcTab.setSelectionModel(selectionModel);
		selectionModel
				.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {

					public void onSelectionChange(SelectionChangeEvent event) {
						final Rfc selRfc = selectionModel.getSelectedObject();
						if (selRfc != null) {

							// Open RFC Single View
							// -------------------------------------------------------------

							ScrollPanel sp = new ScrollPanel();
							sp.setSize("420px", "400px");

							final DialogBox rfcBox = new DialogBox();
							rfcBox.setText("RFC: " + selRfc.getId()
									+ " - Details");

							final Button closeWindow = new Button("Close");
							final Button btnApproveRfc = new Button("Approve");
							final Button btnRejectRfc = new Button("Reject");
							final Button btnChangeRfc = new Button("Change");
							final Button btnSaveRfc = new Button("Save");
							final Button btnCancelRfc = new Button("Cancel");
							final Button btnAuthRfc = new Button("Authorize");
							final Button btnDelegateRfc = new Button("Delegate");
							final Button btnInProgRfc = new Button(
									"In Progress");
							final Button btnSolvedRfc = new Button("Solved");
							final Button btnClosedRfc = new Button("Close RFC");

							final ListBox lbCD = new ListBox();
							lbCD.setVisible(false);
							lbCD.setVisibleItemCount(1);
							dbService.getCD(new AsyncCallback<String[]>() {
								public void onFailure(Throwable caught) {
									// Show the RPC error message to the user
									dialogBox
											.setText("Remote Procedure Call - Failure");
									serverResponseLabel
											.addStyleName("serverResponseLabelError");
									serverResponseLabel.setHTML(SERVER_ERROR);
									dialogBox.center();
									closeButton.setFocus(true);
								}

								// Refresh User Listbox
								public void onSuccess(String[] result) {
									lbCD.clear();
									int i = 0;
									while (result[i] != null) {
										lbCD.addItem((String) result[i]);
										i++;
									}
								}
							});

							lbCD.addChangeHandler(new ChangeHandler() {

								@Override
								public void onChange(ChangeEvent event) {
									if (lbCD.isItemSelected(lbCD
											.getSelectedIndex())) {
										selRfc.setCD(lbCD.getItemText(lbCD
												.getSelectedIndex()));
									}
								}

							});

							// In Approval
							if (selRfc.getState().equals("CREATED") == true
									&& user.role.equals("Change Approver") == true
									&& user.uname.equals(selRfc.getCA()) == true) {
								btnApproveRfc.setVisible(true);
								btnRejectRfc.setVisible(true);
								btnChangeRfc.setVisible(false);
								btnSaveRfc.setVisible(false);
								btnAuthRfc.setVisible(false);
								btnDelegateRfc.setVisible(false);
								btnInProgRfc.setVisible(false);
								btnSolvedRfc.setVisible(false);
								btnClosedRfc.setVisible(false);
								btnCancelRfc.setVisible(false);
							}

							// Is Approved
							if (selRfc.getState().equals("APPROVED") == true
									&& user.role.equals("Change Owner") == true
									&& user.uname.equals(selRfc.getCO()) == true) {
								btnChangeRfc.setVisible(true);
								btnAuthRfc.setVisible(true);
								btnCancelRfc.setVisible(true);
								btnSaveRfc.setVisible(true);
								btnApproveRfc.setVisible(false);
								btnRejectRfc.setVisible(false);
								btnDelegateRfc.setVisible(false);
								btnInProgRfc.setVisible(false);
								btnSolvedRfc.setVisible(false);
								btnClosedRfc.setVisible(false);
							}
							// Is Authorized
							if (selRfc.getState().equals("AUTHORIZED") == true
									&& user.role.equals("Change Owner") == true
									&& user.uname.equals(selRfc.getCO()) == true) {
								btnChangeRfc.setVisible(true);
								btnAuthRfc.setVisible(false);
								btnCancelRfc.setVisible(true);
								btnSaveRfc.setVisible(true);
								btnDelegateRfc.setVisible(true);
								btnApproveRfc.setVisible(false);
								btnRejectRfc.setVisible(false);
								btnInProgRfc.setVisible(false);
								btnSolvedRfc.setVisible(false);
								btnClosedRfc.setVisible(false);
								lbCD.setVisible(true);
							}

							// Is Delegated
							if (selRfc.getState().equals("DELEGATED") == true
									&& user.role.equals("Change Deployer") == true
									&& user.uname.equals(selRfc.getCD()) == true) {
								btnChangeRfc.setVisible(true);
								btnAuthRfc.setVisible(false);
								btnCancelRfc.setVisible(true);
								btnSaveRfc.setVisible(true);
								btnDelegateRfc.setVisible(false);
								btnApproveRfc.setVisible(false);
								btnRejectRfc.setVisible(false);
								btnInProgRfc.setVisible(true);
								btnSolvedRfc.setVisible(false);
								btnClosedRfc.setVisible(false);
							}

							// Is In Progress
							if (selRfc.getState().equals("INPROGRESS") == true
									&& user.role.equals("Change Deployer") == true
									&& user.uname.equals(selRfc.getCD()) == true) {
								btnChangeRfc.setVisible(true);
								btnAuthRfc.setVisible(false);
								btnCancelRfc.setVisible(true);
								btnSaveRfc.setVisible(true);
								btnDelegateRfc.setVisible(false);
								btnApproveRfc.setVisible(false);
								btnRejectRfc.setVisible(false);
								btnInProgRfc.setVisible(false);
								btnSolvedRfc.setVisible(true);
								btnClosedRfc.setVisible(false);
							}

							// Is Solved
							if (selRfc.getState().equals("SOLVED") == true
									&& user.role.equals("Change Owner") == true
									&& user.uname.equals(selRfc.getCO()) == true) {
								btnChangeRfc.setVisible(false);
								btnAuthRfc.setVisible(false);
								btnCancelRfc.setVisible(false);
								btnSaveRfc.setVisible(false);
								btnDelegateRfc.setVisible(true);
								btnApproveRfc.setVisible(false);
								btnRejectRfc.setVisible(false);
								btnInProgRfc.setVisible(false);
								btnSolvedRfc.setVisible(false);
								btnClosedRfc.setVisible(true);
							}

							// Change Owner not Approved
							// rejected or closed
							if ((user.role.equals("Change Owner") == true && selRfc
									.getState().equals("CREATED") == true)

									|| (user.role.equals("Change Owner") == true && user.uname
											.equals(selRfc.getCO()) == false)

									|| (user.role.equals("Change Approver") == true && user.uname
											.equals(selRfc.getCA()) == false)

									|| (user.role.equals("Change Deployer") == true && user.uname
											.equals(selRfc.getCD()) == false)

							) {
								btnApproveRfc.setVisible(false);
								btnRejectRfc.setVisible(false);
								btnChangeRfc.setVisible(false);
								btnSaveRfc.setVisible(false);
								btnAuthRfc.setVisible(false);
								btnDelegateRfc.setVisible(false);
								btnInProgRfc.setVisible(false);
								btnSolvedRfc.setVisible(false);
								btnClosedRfc.setVisible(false);
								btnCancelRfc.setVisible(false);
							}

							if (user.role.equals("Administrator") == true) {
								btnApproveRfc.setVisible(true);
								btnRejectRfc.setVisible(true);
								btnChangeRfc.setVisible(true);
								btnSaveRfc.setVisible(true);
								btnAuthRfc.setVisible(true);
								btnDelegateRfc.setVisible(true);
								btnInProgRfc.setVisible(true);
								btnSolvedRfc.setVisible(true);
								btnClosedRfc.setVisible(true);
								btnCancelRfc.setVisible(true);
							}

							// Normal User only show details or state canceld or
							// rejected or closed
							if (user.role.equals("Change User") == true
									|| user.role.equals("Change Initiator") == true
									|| selRfc.getState().equals("REJECTED")
									|| selRfc.getState().equals("CANCELD")
									|| selRfc.getState().equals("CLOSED")
									|| (user.role.equals("Change Approver") == true && selRfc
											.getState().equals("CREATED") == false)
									|| (user.role.equals("Change Owner") == true && (selRfc
											.getState().equals("DELEGATED") == true || selRfc
											.getState().equals("INPROGRESS") == true))
									|| (user.role.equals("Change Deployer") == true && selRfc
											.getState().equals("SOLVED") == true)

							) {
								btnApproveRfc.setVisible(false);
								btnRejectRfc.setVisible(false);
								btnChangeRfc.setVisible(false);
								btnSaveRfc.setVisible(false);
								btnAuthRfc.setVisible(false);
								btnDelegateRfc.setVisible(false);
								btnInProgRfc.setVisible(false);
								btnSolvedRfc.setVisible(false);
								btnClosedRfc.setVisible(false);
								btnCancelRfc.setVisible(false);
							}

							// TextBoxes
							final LongBox IDRFC = new LongBox();
							IDRFC.setValue(selRfc.getId());
							IDRFC.setEnabled(false);
							final TextBox DescRFC = new TextBox();
							DescRFC.setText(selRfc.getDesc());
							DescRFC.setEnabled(false);
							final TextBox ReasonRFC = new TextBox();
							ReasonRFC.setText(selRfc.getReason());
							ReasonRFC.setEnabled(false);
							final IntegerBox CostsRFC = new IntegerBox();
							CostsRFC.setText(selRfc.getCost());
							CostsRFC.setEnabled(false);

							final TextBox CI = new TextBox();
							CI.setText(selRfc.getCI());
							CI.setEnabled(false);

							final TextBox CA = new TextBox();
							CA.setText(selRfc.getCA());
							CA.setEnabled(false);

							final TextBox CO = new TextBox();
							CO.setText(selRfc.getCO());
							CO.setEnabled(false);

							final TextBox CD = new TextBox();
							CD.setText(selRfc.getCD());
							CD.setEnabled(false);

							// TextAreas
							final TextArea DetailDescRFC = new TextArea();
							DetailDescRFC.setText(selRfc.getDetailDesc());
							DetailDescRFC.setEnabled(false);
							final TextArea BenefitRFC = new TextArea();
							BenefitRFC.setText(selRfc.getBenefit());
							BenefitRFC.setEnabled(false);
							final TextArea RisksRFC = new TextArea();
							RisksRFC.setText(selRfc.getRisk());
							RisksRFC.setEnabled(false);

							final TextArea service = new TextArea();
							service.setText(selRfc.getServ());
							service.setEnabled(false);
							final TextArea cItem = new TextArea();
							cItem.setText(selRfc.getcItem());
							cItem.setEnabled(false);
							final TextArea state = new TextArea();
							state.setText(selRfc.getState());
							state.setEnabled(false);
							final TextArea prio = new TextArea();
							prio.setText(selRfc.getPrio());
							prio.setEnabled(false);

							// DateBoxes
							final DateBox validFrom = new DateBox();
							validFrom.setValue(selRfc.getValidFrom());
							validFrom.setEnabled(false);

							final DateBox approvedOn = new DateBox();
							approvedOn.setValue(selRfc.getApprovedOn());
							approvedOn.setEnabled(false);

							final DateBox solvedOn = new DateBox();
							solvedOn.setValue(selRfc.getSolvedOn());
							solvedOn.setEnabled(false);

							final DateBox closedOn = new DateBox();
							closedOn.setValue(selRfc.getClosedOn());
							closedOn.getDatePicker();
							closedOn.setEnabled(false);

							final DateBox delegatedOn = new DateBox();
							delegatedOn.setValue(selRfc.getDelegatedOn());
							delegatedOn.setEnabled(false);

							// We can set the id of a widget by accessing its
							// Element
							closeWindow.getElement().setId("closeRFC");
							final VerticalPanel rfcPanel = new VerticalPanel();
							rfcPanel.addStyleName("RfcPanel");

							final HorizontalPanel btnPanel = new HorizontalPanel();
							btnPanel.add(btnApproveRfc);
							btnPanel.add(btnRejectRfc);
							btnPanel.add(btnChangeRfc);
							btnPanel.add(btnSaveRfc);
							btnPanel.add(btnChangeRfc);
							btnPanel.add(btnAuthRfc);
							btnPanel.add(btnCancelRfc);
							btnPanel.add(btnDelegateRfc);
							btnPanel.add(btnInProgRfc);
							btnPanel.add(btnSolvedRfc);
							btnPanel.add(btnClosedRfc);
							btnPanel.add(closeWindow);

							// New Grid Item to List description texts with
							// according Widget
							Grid grid = new Grid(20, 2);
							grid.setTitle("RFC Overview");
							grid.setText(0, 0, "RFC ID");

							grid.setWidget(0, 1, IDRFC);
							grid.setText(1, 0, "Summary");
							grid.setWidget(1, 1, DescRFC);
							grid.setText(2, 0, "Detail Description");
							grid.setWidget(2, 1, DetailDescRFC);
							grid.setText(3, 0, "Date of submission");
							grid.setWidget(3, 1, validFrom);
							grid.setText(4, 0, "Change Initiator");
							grid.setWidget(4, 1, CI);
							grid.setText(5, 0, "Change Owner");
							grid.setWidget(5, 1, CO);
							grid.setText(6, 0, "Change Approver");
							grid.setWidget(6, 1, CA);

							if (selRfc.getState().equals("AUTHORIZED")) {
								grid.setText(7, 0, "Change Deployer");
								grid.setWidget(7, 1, lbCD);
							} else {
								grid.setText(7, 0, "Change Deployer");
								grid.setWidget(7, 1, CD);
							}

							grid.setText(8, 0, "Change Priority");
							grid.setWidget(8, 1, prio);
							grid.setText(9, 0, "Reason for the Change");
							grid.setWidget(9, 1, ReasonRFC);
							grid.setText(10, 0, "Costs (CHF)");
							grid.setWidget(10, 1, CostsRFC);
							grid.setText(11, 0, "Benefits");
							grid.setWidget(11, 1, BenefitRFC);
							grid.setText(12, 0, "Risks");
							grid.setWidget(12, 1, RisksRFC);
							grid.setText(13, 0, "Affected Services");
							grid.setWidget(13, 1, service);
							grid.setText(14, 0, "Affected CI's");
							grid.setWidget(14, 1, cItem);
							grid.setText(15, 0, "State");
							grid.setWidget(15, 1, state);
							grid.setText(16, 0, "Approved on");
							grid.setWidget(16, 1, approvedOn);
							grid.setText(17, 0, "Soved on");
							grid.setWidget(17, 1, solvedOn);
							grid.setText(18, 0, "Closed on");
							grid.setWidget(18, 1, closedOn);
							// grid.setStyleName("RFCCreate");

							rfcPanel.add(btnPanel);
							rfcPanel.add(grid);
							sp.setWidget(rfcPanel);
							rfcBox.setWidget(sp);
							rfcBox.center();

							// Add a handler CLOSE
							closeWindow.addClickHandler(new ClickHandler() {
								public void onClick(ClickEvent event) {
									rfcBox.hide();
									selectionModel.setSelected(selRfc, false);
								}
							});
							// Add a handler APPROVE
							btnApproveRfc.addClickHandler(new ClickHandler() {
								public void onClick(ClickEvent event) {
									selRfc.setState("APPROVED");
									state.setText("APPROVED");
									selRfc.setApprovedOn(new Date());
									approvedOn.setValue(selRfc.getApprovedOn());
									// Update ListView
									rfcTab.setRowCount(RfcList.size(), true);
									rfcTab.setRowData(0, RfcList);
									changeRfc(selRfc);

								}
							});

							// Add a handler AUTHORIZE
							btnAuthRfc.addClickHandler(new ClickHandler() {
								public void onClick(ClickEvent event) {
									selRfc.setState("AUTHORIZED");
									state.setText("AUTHORIZED");
									lbCD.setVisible(true);

									createWO(selRfc);
									// Update ListView
									rfcTab.setRowCount(RfcList.size(), true);
									rfcTab.setRowData(0, RfcList);
									changeRfc(selRfc);
								}
							});

							// Add a handler REJECT
							btnRejectRfc.addClickHandler(new ClickHandler() {
								public void onClick(ClickEvent event) {
									selRfc.setState("REJECTED");
									state.setText("REJECTED");
									// Update ListView
									rfcTab.setRowCount(RfcList.size(), true);
									rfcTab.setRowData(0, RfcList);
									changeRfc(selRfc);
								}
							});

							// Add a handler CHANGE
							btnChangeRfc.addClickHandler(new ClickHandler() {
								public void onClick(ClickEvent event) {

									DescRFC.setEnabled(true);
									ReasonRFC.setEnabled(true);
									CostsRFC.setEnabled(true);
									DetailDescRFC.setEnabled(true);
									BenefitRFC.setEnabled(true);
									RisksRFC.setEnabled(true);
									service.setEnabled(true);
									cItem.setEnabled(true);
									prio.setEnabled(true);

								}
							});

							// Add a handler SAVE
							btnSaveRfc.addClickHandler(new ClickHandler() {
								public void onClick(ClickEvent event) {

									DescRFC.setEnabled(false);
									ReasonRFC.setEnabled(false);
									CostsRFC.setEnabled(false);
									DetailDescRFC.setEnabled(false);
									BenefitRFC.setEnabled(false);
									RisksRFC.setEnabled(false);
									service.setEnabled(false);
									cItem.setEnabled(false);
									prio.setEnabled(false);

									selRfc.setDesc(DescRFC.getText());
									selRfc.setDetailDesc(DetailDescRFC
											.getText());
									selRfc.setReason(ReasonRFC.getText());
									selRfc.setCost(CostsRFC.getText());
									selRfc.setBenefit(BenefitRFC.getText());
									selRfc.setRisk(RisksRFC.getText());
									selRfc.setServ(service.getText());
									selRfc.setcItem(cItem.getText());
									selRfc.setPrio(prio.getText());

									// Update ListView
									rfcTab.setRowCount(RfcList.size(), true);
									rfcTab.setRowData(0, RfcList);

									changeRfc(selRfc);

								}
							});

							// Add a handler DELEGATE
							btnDelegateRfc.addClickHandler(new ClickHandler() {
								public void onClick(ClickEvent event) {
									selRfc.setState("DELEGATED");
									state.setText("DELEGATED");
									selRfc.setCD(lbCD.getItemText(lbCD
											.getSelectedIndex()));
									CD.setText(selRfc.getCD());
									selRfc.setDelegatedOn(new Date());
									delegatedOn.setValue(selRfc
											.getDelegatedOn());

									// Update ListView
									rfcTab.setRowCount(RfcList.size(), true);
									rfcTab.setRowData(0, RfcList);

									dbService.getWO(selRfc.getId(),
											(new AsyncCallback<WO>() {

												@Override
												public void onFailure(
														Throwable caught) {
													dialogBox
															.setText("Failure");
													dialogVPanel.clear();
													dialogVPanel
															.add(new HTML(
																	"<b> Update Error </b>"));
													dialogBox.center();
													dialogVPanel
															.add(closeButton);
													closeButton.setFocus(true);
												}

												@Override
												public void onSuccess(WO result) {
													if (result != null) {
														result.setCD(selRfc
																.getCD());
														changeWO(result);
														// Update ListView

														woTab.setVisibleRangeAndClearData(
																woTab.getVisibleRange(),
																true);
														woTab.setRowCount(
																WOList.size(),
																true);
														woTab.setRowData(0,
																WOList);

													}
												}
											}));
									changeRfc(selRfc);
								}
							});

							// Add a handler IN PROGRESS
							btnInProgRfc.addClickHandler(new ClickHandler() {
								public void onClick(ClickEvent event) {
									selRfc.setState("INPROGRESS");
									state.setText("INPROGRESS");
									// Update ListView
									rfcTab.setRowCount(RfcList.size(), true);
									rfcTab.setRowData(0, RfcList);
									changeRfc(selRfc);
								}
							});

							// Add a handler SOLVED
							btnSolvedRfc.addClickHandler(new ClickHandler() {
								public void onClick(ClickEvent event) {
									selRfc.setState("SOLVED");
									state.setText("SOLVED");
									selRfc.setSolvedOn(new Date());
									solvedOn.setValue(selRfc.getSolvedOn());
									// Update ListView
									rfcTab.setRowCount(RfcList.size(), true);
									rfcTab.setRowData(0, RfcList);
									changeRfc(selRfc);
								}
							});

							// Add a handler CLOSED
							btnClosedRfc.addClickHandler(new ClickHandler() {
								public void onClick(ClickEvent event) {
									selRfc.setState("CLOSED");
									state.setText("CLOSED");
									selRfc.setClosedOn(new Date());
									closedOn.setValue(selRfc.getClosedOn());
									// Update ListView
									rfcTab.setRowCount(RfcList.size(), true);
									rfcTab.setRowData(0, RfcList);
									changeRfc(selRfc);

								}
							});

						}

					}

					private void changeRfc(Rfc rfc) {

						// Write new RFC in the Datastore
						dbService.changeRfc(rfc.getId(), rfc.getReason(),
								rfc.getDesc(), rfc.getDetailDesc(),
								rfc.getValidFrom(), rfc.getApprovedOn(),
								rfc.getSolvedOn(), rfc.getClosedOn(),
								rfc.getStartedOn(), rfc.getCI(), rfc.getCO(),
								rfc.getCA(), rfc.getCD(), rfc.getPrio(),
								rfc.getCost(), rfc.getBenefit(), rfc.getRisk(),
								rfc.getServ(), rfc.getcItem(), rfc.getState(),

								(new AsyncCallback<String>() {

									@Override
									public void onFailure(Throwable caught) {
										dialogBox.setText("Failure");
										dialogVPanel.clear();
										dialogVPanel.add(new HTML(
												"<b> Update Error </b>"));
										dialogBox.center();
										dialogVPanel.add(closeButton);
										closeButton.setFocus(true);

									}

									@Override
									public void onSuccess(String result) {

										dialogBox.setText("Success");
										dialogVPanel.clear();
										dialogVPanel.add(new HTML("<b>"
												+ result + "</b>"));
										dialogBox.center();
										dialogVPanel.add(closeButton);
									}

								}));
					}

					private void createWO(Rfc rfc) {

						// Create the new WO and add it to the list
						WOList.add(new WO(rfc.getId(), rfc.getValidFrom(),
								null, null, null, null, rfc.getCD(), rfc
										.getCO(), "Task", rfc.getDesc(), rfc
										.getServ(), rfc.getcItem(), "INITIAL"));

						// Update ListView
						woTab.setRowCount(WOList.size(), true);
						woTab.setRowData(0, WOList);

						dbService.createWO(rfc.getId(), rfc.getValidFrom(),
								rfc.getCD(), rfc.getCO(), "Task",
								rfc.getDesc(), rfc.getServ(), rfc.getcItem(),
								(new AsyncCallback<String>() {

									@Override
									public void onFailure(Throwable caught) {
										dialogBox.setText("Failure");
										dialogVPanel.clear();
										dialogVPanel.add(new HTML(
												"<b> Update Error </b>"));
										dialogBox.center();
										dialogVPanel.add(closeButton);
										closeButton.setFocus(true);

									}

									@Override
									public void onSuccess(String result) {

										dialogBox.setText("Success");
										dialogVPanel.clear();
										dialogVPanel.add(new HTML("<b>"
												+ result + "</b>"));
										dialogBox.center();
										dialogVPanel.add(closeButton);
									}

								}));

					}

					private void changeWO(WO wo) {

						// Write new RFC in the Datastore
						dbService.changeWO(wo.getId(), wo.getPlanedStart(),
								wo.getPlanedEnd(), wo.getActStart(),
								wo.getActEnd(), wo.getPlanedEffort(),
								wo.getCD(), wo.getCO(), wo.getWoType(),
								wo.getDesc(), wo.getServ(), wo.getcItem(),
								wo.getState(), (new AsyncCallback<String>() {

									@Override
									public void onFailure(Throwable caught) {
										dialogBox.setText("Failure");
										dialogVPanel.clear();
										dialogVPanel.add(new HTML(
												"<b> Update Error </b>"));
										dialogBox.center();
										dialogVPanel.add(closeButton);
										closeButton.setFocus(true);

									}

									@Override
									public void onSuccess(String result) {

										dialogBox.setText("Success");
										dialogVPanel.clear();
										dialogVPanel.add(new HTML("<b>"
												+ result + "</b>"));
										dialogBox.center();
										dialogVPanel.add(closeButton);
									}

								}));
					}

				});

		// End of RFC Single View
		// -------------------------------------------------------------

		TextColumn<Rfc> col_id = new TextColumn<Rfc>() {

			public String getValue(Rfc object) {
				return Long.toString(object.getId());
			}
		};
		// Make the name column sortable.
		col_id.setSortable(true);
		rfcTab.addColumn(col_id, "RFC ID");

		TextColumn<Rfc> col_desc = new TextColumn<Rfc>() {
			public String getValue(Rfc object) {
				return object.getDesc();
			}
		};
		rfcTab.addColumn(col_desc, "Description");

		TextColumn<Rfc> col_co = new TextColumn<Rfc>() {
			public String getValue(Rfc object) {
				return object.getCO();
			}
		};
		rfcTab.addColumn(col_co, "Change Owner");

		TextColumn<Rfc> col_prio = new TextColumn<Rfc>() {
			public String getValue(Rfc object) {
				return object.getPrio();
			}
		};
		rfcTab.addColumn(col_prio, "Priority");

		TextColumn<Rfc> col_state = new TextColumn<Rfc>() {
			public String getValue(Rfc object) {
				return object.getState();
			}
		};
		rfcTab.addColumn(col_state, "State");

		DateCell dateCell = new DateCell();
		Column<Rfc, Date> col_validFrom = new Column<Rfc, Date>(dateCell) {
			public Date getValue(Rfc object) {
				return object.getValidFrom();
			}
		};
		rfcTab.addColumn(col_validFrom, "Valid From");

		// Associate an async data provider to the table
		AsyncDataProvider<Rfc> provider = new AsyncDataProvider<Rfc>() {

			@Override
			protected void onRangeChanged(HasData<Rfc> display) {
				final int start = display.getVisibleRange().getStart();
				int length = display.getVisibleRange().getLength();
				AsyncCallback<List<Rfc>> callback = new AsyncCallback<List<Rfc>>() {
					@Override
					public void onFailure(Throwable caught) {
						dialogBox.setText("Remote Procedure Call - Failure");
						serverResponseLabel
								.addStyleName("serverResponseLabelError");
						serverResponseLabel.setHTML(SERVER_ERROR);
						dialogBox.center();
						closeButton.setFocus(true);
					}

					@Override
					public void onSuccess(List<Rfc> result) {
						updateRowData(start, result);
						RfcList = result;
					}

				};
				// The remote service that should be implemented
				dbService.fetchRfc(start, length, callback);
			}
		};
		provider.addDataDisplay(rfcTab);
		provider.updateRowCount(RfcList.size(), true);

		SimplePager pager = new SimplePager();
		pager.setDisplay(rfcTab);

		// Add a ColumnSortEvent.AsyncHandler to connect sorting to the
		// AsyncDataPRrovider.

		// --> Sort yet not finished
		// AsyncHandler columnSortHandler = new AsyncHandler(rfcTab);
		// rfcTab.addColumnSortHandler(new ColumnSortEvent.Handler() {
		// public void onColumnSort(ColumnSortEvent event) {
		// List<Rfc> newData = new ArrayList(rfcTab.getVisibleItems()); // Copy
		// the data
		// if (event.isSortAscending) {
		// Collections.sort(newData, myAscComparator); // Sort ascending.
		// } else {
		// Collections.sort(newData, myDescComparator); // Sort descending.
		// }
		// cellTable.setRowData(cellTable.getVisibleRange(), newData);
		// }
		// });

		vp = new VerticalPanel();
		vp.add(rfcTab);
		vp.add(pager);

		dp.add(new Label(text2), DockPanel.NORTH);
		dp.add(vp, DockPanel.CENTER);

		panel.add(dp, "Show RFCs");

		// //////// TAB - Work Orders ---------------------------------//
		dp = new DockPanel();

		final SingleSelectionModel<WO> selModel = new SingleSelectionModel<WO>();
		woTab.setSelectionModel(selModel);
		selModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {

			public void onSelectionChange(SelectionChangeEvent event) {
				final WO selWO = selModel.getSelectedObject();
				if (selWO != null) {

					// Open WO Single View
					// -------------------------------------------------------------

					ScrollPanel sp = new ScrollPanel();
					sp.setSize("420px", "400px");

					final DialogBox woBox = new DialogBox();
					woBox.setText("WO: " + selWO.getId() + " - Details");

					final Button closeWindow = new Button("Close");
					final Button btnChangeWO = new Button("Change");
					final Button btnSaveWO = new Button("Save");
					final Button btnInProgWO = new Button("In Progress");
					final Button btnCancelWO = new Button("Cancel");
					final Button btnClosedWO = new Button("Close WO");

					btnChangeWO.setVisible(false);
					btnSaveWO.setVisible(false);
					btnInProgWO.setVisible(false);
					btnClosedWO.setVisible(false);
					btnCancelWO.setVisible(false);

					// Inital
					if (selWO.getState().equals("INITIAL") == true
							&& (user.role.equals("Change Owner") == true && user.uname
									.equals(selWO.getCO()) == true)) {
						btnChangeWO.setVisible(true);
						btnSaveWO.setVisible(true);
						btnCancelWO.setVisible(true);
					}

					// Inital
					if (selWO.getState().equals("INITIAL") == true
							&& (user.role.equals("Change Deployer") == true && user.uname
									.equals(selWO.getCD()) == true)) {
						btnInProgWO.setVisible(true);
						btnChangeWO.setVisible(true);
						btnSaveWO.setVisible(true);
						btnCancelWO.setVisible(true);
					}

					// Is Progress
					if (selWO.getState().equals("INPROGRESS") == true
							&& (user.role.equals("Change Deployer") == true && user.uname
									.equals(selWO.getCD()) == true)) {

						btnInProgWO.setVisible(false);
						btnChangeWO.setVisible(true);
						btnSaveWO.setVisible(true);
						btnClosedWO.setVisible(true);
						btnCancelWO.setVisible(true);
					}
					// Admin
					if (user.role.equals("Administrator") == true) {
						btnInProgWO.setVisible(true);
						btnChangeWO.setVisible(true);
						btnSaveWO.setVisible(true);
						btnClosedWO.setVisible(true);
						btnCancelWO.setVisible(true);
					}

					// Closed or Canceld
					if (selWO.getState().equals("CLOSED") == true
							|| selWO.getState().equals("CANCELD") == true) {
						btnInProgWO.setVisible(false);
						btnChangeWO.setVisible(false);
						btnSaveWO.setVisible(false);
						btnClosedWO.setVisible(false);
						btnCancelWO.setVisible(false);
					}

					// TextBoxes
					final LongBox IDWO = new LongBox();
					IDWO.setValue(selWO.getId());
					IDWO.setEnabled(false);

					// TextAreas
					final TextBox CO = new TextBox();
					CO.setText(selWO.getCO());
					CO.setEnabled(false);
					final TextBox CD = new TextBox();
					CD.setText(selWO.getCD());
					CD.setEnabled(false);
					final TextBox DescWO = new TextBox();
					DescWO.setText(selWO.getDesc());
					DescWO.setEnabled(false);
					final TextBox woType = new TextBox();
					woType.setText(selWO.getWoType());
					woType.setEnabled(false);
					final TextArea service = new TextArea();
					service.setText(selWO.getServ());
					service.setEnabled(false);
					final TextArea cItem = new TextArea();
					cItem.setText(selWO.getcItem());
					cItem.setEnabled(false);
					final TextArea state = new TextArea();
					state.setText(selWO.getState());
					state.setEnabled(false);
					final TextArea planedEffort = new TextArea();
					planedEffort.setEnabled(false);
					planedEffort.setText(selWO.getPlanedEffort());

					// DateBoxes
					final DateBox planedStart = new DateBox();
					planedStart.setValue(selWO.getPlanedStart());
					planedStart.setEnabled(false);

					final DateBox planedEnd = new DateBox();
					planedEnd.getDatePicker();
					planedEnd.setEnabled(false);

					final DateBox actStart = new DateBox();
					actStart.setValue(selWO.getActStart());
					actStart.setEnabled(false);

					final DateBox actEnd = new DateBox();
					actEnd.setValue(selWO.getActEnd());
					actEnd.getDatePicker();
					actEnd.setEnabled(false);

					final VerticalPanel woPanel = new VerticalPanel();
					woPanel.addStyleName("RfcPanel");

					final HorizontalPanel btnPanel = new HorizontalPanel();
					btnPanel.add(btnChangeWO);
					btnPanel.add(btnSaveWO);
					btnPanel.add(btnInProgWO);
					btnPanel.add(btnClosedWO);
					btnPanel.add(btnCancelWO);
					btnPanel.add(closeWindow);

					// New Grid Item to List description texts with
					// according Widget
					Grid grid = new Grid(12, 2);
					grid.setTitle("RFC Overview");
					grid.setText(0, 0, "RFC ID");
					grid.setWidget(0, 1, IDWO);
					grid.setText(1, 0, "WO Type");
					grid.setWidget(1, 1, woType);
					grid.setText(2, 0, "Description");
					grid.setWidget(2, 1, DescWO);

					grid.setText(3, 0, "Change Deployer");
					grid.setWidget(3, 1, CD);
					grid.setText(4, 0, "Change Owner");
					grid.setWidget(4, 1, CO);

					grid.setText(5, 0, "Planed Start");
					grid.setWidget(5, 1, planedStart);

					grid.setText(6, 0, "Planed End");
					grid.setWidget(6, 1, planedEnd);

					grid.setText(7, 0, "Actual Start");
					grid.setWidget(7, 1, actStart);

					grid.setText(8, 0, "Actual End");
					grid.setWidget(8, 1, actEnd);

					grid.setText(9, 0, "Planed Effort");
					grid.setWidget(9, 1, planedEffort);
					grid.setText(10, 0, "State");
					grid.setWidget(10, 1, state);

					woPanel.add(btnPanel);
					woPanel.add(grid);
					sp.setWidget(woPanel);
					woBox.setWidget(sp);
					woBox.center();

					// Add a handler CLOSE
					closeWindow.addClickHandler(new ClickHandler() {
						public void onClick(ClickEvent event) {
							woBox.hide();
							selModel.setSelected(selWO, false);
						}
					});

					// Add a handler CHANGE
					btnChangeWO.addClickHandler(new ClickHandler() {
						public void onClick(ClickEvent event) {

							DescWO.setEnabled(true);
							woType.setEnabled(true);
							service.setEnabled(true);
							cItem.setEnabled(true);
							state.setEnabled(true);
							planedEffort.setEnabled(true);
							planedStart.setEnabled(true);
							planedEnd.setEnabled(true);
							actStart.setEnabled(true);
							actEnd.setEnabled(true);

						}
					});

					// Add a handler InProgress
					btnInProgWO.addClickHandler(new ClickHandler() {
						public void onClick(ClickEvent event) {

							state.setText("INPROGRESS");
							selWO.setState("INPROGRESS");
							selWO.setActStart(new Date());
							actStart.setValue(selWO.getActStart());

							selWO.setDesc(DescWO.getText());
							selWO.setServ(service.getText());
							selWO.setcItem(cItem.getText());

							// Update ListView
							rfcTab.setRowCount(RfcList.size(), true);
							rfcTab.setRowData(0, RfcList);

							changeWO(selWO);

						}
					});

					// Add a handler SAVE
					btnSaveWO.addClickHandler(new ClickHandler() {
						public void onClick(ClickEvent event) {

							DescWO.setEnabled(false);
							woType.setEnabled(false);
							service.setEnabled(false);
							cItem.setEnabled(false);
							state.setEnabled(false);
							planedEffort.setEnabled(false);
							planedStart.setEnabled(false);
							planedEnd.setEnabled(false);
							actStart.setEnabled(false);
							actEnd.setEnabled(false);

							selWO.setDesc(DescWO.getText());
							selWO.setServ(service.getText());
							selWO.setcItem(cItem.getText());

							// Update ListView
							rfcTab.setRowCount(RfcList.size(), true);
							rfcTab.setRowData(0, RfcList);

							changeWO(selWO);

						}
					});

					// Add a handler CLOSED
					btnClosedWO.addClickHandler(new ClickHandler() {
						public void onClick(ClickEvent event) {
							selWO.setState("CLOSED");
							state.setText("CLOSED");
							selWO.setActEnd(new Date());
							actEnd.setValue(selWO.getActEnd());
							// Update ListView
							rfcTab.setRowCount(RfcList.size(), true);
							rfcTab.setRowData(0, RfcList);
							changeWO(selWO);
						}
					});

					// Add a handler CLOSED
					btnCancelWO.addClickHandler(new ClickHandler() {
						public void onClick(ClickEvent event) {
							selWO.setState("CANCEL");
							state.setText("CANCEL");
							selWO.setActEnd(new Date());
							actEnd.setValue(selWO.getActEnd());
							// Update ListView
							rfcTab.setRowCount(RfcList.size(), true);
							rfcTab.setRowData(0, RfcList);
							changeWO(selWO);
						}
					});

				}

			}

			private void changeWO(WO wo) {

				// Write new RFC in the Datastore
				dbService.changeWO(wo.getId(), wo.getPlanedStart(),
						wo.getPlanedEnd(), wo.getActStart(), wo.getActEnd(),
						wo.getPlanedEffort(), wo.getCD(), wo.getCO(),
						wo.getWoType(), wo.getDesc(), wo.getServ(),
						wo.getcItem(), wo.getState(),
						(new AsyncCallback<String>() {

							@Override
							public void onFailure(Throwable caught) {
								dialogBox.setText("Failure");
								dialogVPanel.clear();
								dialogVPanel.add(new HTML(
										"<b> Update Error </b>"));
								dialogBox.center();
								dialogVPanel.add(closeButton);
								closeButton.setFocus(true);

							}

							@Override
							public void onSuccess(String result) {

								dialogBox.setText("Success");
								dialogVPanel.clear();
								dialogVPanel.add(new HTML("<b>" + result
										+ "</b>"));
								dialogBox.center();
								dialogVPanel.add(closeButton);
							}

						}));
			}

		});

		// End of RFC Single View
		// -------------------------------------------------------------

		TextColumn<WO> wo_id = new TextColumn<WO>() {

			public String getValue(WO object) {
				return Long.toString(object.getId());
			}
		};
		// Make the name column sortable.
		wo_id.setSortable(true);
		woTab.addColumn(wo_id, "WO ID");

		TextColumn<WO> wo_desc = new TextColumn<WO>() {
			public String getValue(WO object) {
				return object.getDesc();
			}
		};
		woTab.addColumn(wo_desc, "Description");

		TextColumn<WO> wo_cd = new TextColumn<WO>() {
			public String getValue(WO object) {
				return object.getCD();
			}
		};
		woTab.addColumn(wo_cd, "Change Deployer");

		TextColumn<WO> wo_state = new TextColumn<WO>() {
			public String getValue(WO object) {
				return object.getState();
			}
		};
		woTab.addColumn(wo_state, "State");

		dateCell = new DateCell();
		Column<WO, Date> wo_AStart = new Column<WO, Date>(dateCell) {
			public Date getValue(WO object) {
				return object.getActStart();
			}
		};
		woTab.addColumn(wo_AStart, "Start Date");

		// Associate an async data provider to the table
		AsyncDataProvider<WO> prov = new AsyncDataProvider<WO>() {

			@Override
			protected void onRangeChanged(HasData<WO> display) {
				final int start = display.getVisibleRange().getStart();
				int length = display.getVisibleRange().getLength();
				AsyncCallback<List<WO>> callback = new AsyncCallback<List<WO>>() {
					@Override
					public void onFailure(Throwable caught) {
						dialogBox.setText("Remote Procedure Call - Failure");
						serverResponseLabel
								.addStyleName("serverResponseLabelError");
						serverResponseLabel.setHTML(SERVER_ERROR);
						dialogBox.center();
						closeButton.setFocus(true);
					}

					@Override
					public void onSuccess(List<WO> result) {
						updateRowData(start, result);
						WOList = result;
					}

				};
				// The remote service that should be implemented
				dbService.fetchWO(start, length, callback);
			}
		};
		prov.addDataDisplay(woTab);
		prov.updateRowCount(WOList.size(), true);

		SimplePager woPager = new SimplePager();
		woPager.setDisplay(woTab);

		vp = new VerticalPanel();
		vp.add(woTab);
		vp.add(woPager);

		dp.add(vp, DockPanel.CENTER);
		dp.add(new Label(text3), DockPanel.NORTH);
		panel.add(dp, "Work Orders");

		// //////// TAB - Admin Panel ---------------------------------//
		if (user.role.equals("Administrator") == true) {
			dp = new DockPanel();
			dp.add(new Label(text4), DockPanel.NORTH);
			panel.add(dp, "Admin");
		}

		// Buttons
		final Button btnUsrI = new Button("Create");
		final Button btnUsrU = new Button("Update");
		final Button btnUsrD = new Button("Delete");
		final Button btnDelRfc = new Button("Delete All RFCs");
		final Button btnSubmit = new Button("Submit");
		btnUsrI.setSize("87px", "34px");
		btnUsrU.setSize("87px", "34px");
		btnUsrD.setSize("87px", "34px");
		// btnDelRfc.setSize("87px", "34px");
		btnUsrI.addStyleName("Button");
		btnUsrU.addStyleName("Button");
		btnUsrD.addStyleName("Button");
		btnDelRfc.addStyleName("Button");

		// TextBoxes
		final TextBox txtUname = new TextBox();
		final TextBox txtLName = new TextBox();
		final TextBox txtFName = new TextBox();
		final PasswordTextBox txtPwd1 = new PasswordTextBox();

		// Labels
		Label lblUser = new Label();
		lblUser.setText("Usermanagment:");

		// File Upload Part

		// Create a new uploader panel and attach it to the document
		MultiUploader uploader = new MultiUploader();
		// RootPanel.get("default").add(defaultUploader);

		// Add a finish handler which will load the image once the upload
		// finishes
		uploader.addOnFinishUploadHandler(onFinishUploaderHandler);
		uploader.setAutoSubmit(false);

		//
		// VerticalPanel vp_upload = new VerticalPanel();
		//
		// final FormPanel uploadForm = new FormPanel();
		// uploadForm.setAction(UPLOAD_ACTION_URL);
		// uploadForm.setEncoding(FormPanel.ENCODING_MULTIPART);
		// uploadForm.setMethod(FormPanel.METHOD_POST);
		// uploadForm.setWidget(vp_upload);
		//
		//
		// FileUpload upload = new FileUpload();
		// upload.setName("uploadFormElement");
		// vp_upload.add(upload);
		// vp_upload.add(btnSubmit);
		//
		//
		// uploadForm.addSubmitCompleteHandler(new SubmitCompleteHandler() {
		// public void onSubmitComplete(SubmitCompleteEvent event) {
		// String xml = event.getResults();
		//
		// dialogBox.setText("Success");
		// dialogVPanel.clear();
		// dialogVPanel.add(new HTML("<b> Upload completed</b>"));
		// dialogBox.center();
		// dialogVPanel.add(closeButton);
		//
		// }
		// });

		// Listboxes
		final ListBox lbRole = new ListBox();
		lbRole.addItem("Change User");
		lbRole.addItem("Change Initiator");
		lbRole.addItem("Change Owner");
		lbRole.addItem("Change Deployer");
		lbRole.addItem("Change Approver");
		lbRole.addItem("Administrator");

		lbRole.setVisibleItemCount(1);

		final ListBox lbUname = new ListBox();
		lbUname.addItem("");
		lbUname.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {

				dbService.getUsers(new AsyncCallback<String[]>() {
					public void onFailure(Throwable caught) {
						// Show the RPC error message to the user
						dialogBox.setText("Remote Procedure Call - Failure");
						serverResponseLabel
								.addStyleName("serverResponseLabelError");
						serverResponseLabel.setHTML(SERVER_ERROR);
						dialogBox.center();
						closeButton.setFocus(true);
					}

					// Refresh User Listbox
					public void onSuccess(String[] result) {
						lbUname.clear();
						lbUname.addItem("");
						int i = 0;
						while (result[i] != null) {
							lbUname.addItem((String) result[i]);
							i++;
						}
					}
				});

				if (lbUname.isItemSelected(0)) {
					// Empty Entry Selected -> Clear TextBoxes
					txtUname.setText("");
					txtLName.setText("");
					txtFName.setText("");
					txtPwd1.setText("");
				}
			}

		});

		lbUname.addChangeHandler(new ChangeHandler() {

			@Override
			public void onChange(ChangeEvent event) {
				if (lbUname.isItemSelected(lbUname.getSelectedIndex())) {
					// Get Userdetails and fill the TextBoxes
					dbService.getUser(
							lbUname.getItemText(lbUname.getSelectedIndex()),
							new AsyncCallback<String[]>() {
								public void onFailure(Throwable caught) {
									// Show the RPC error message to the
									// user
									dialogBox
											.setText("Userdetails not available");
									serverResponseLabel
											.addStyleName("serverResponseLabelError");
									serverResponseLabel.setHTML(SERVER_ERROR);
									dialogBox.center();
									closeButton.setFocus(true);
								}

								@Override
								public void onSuccess(String[] result) {
									// TODO Auto-generated method stub

									txtUname.setText(result[0]);
									txtLName.setText(result[1]);
									txtFName.setText(result[2]);

									int idx = Integer.parseInt(result[3]);
									lbRole.setSelectedIndex(idx);

									txtPwd1.setText(result[4]);
								}
							});
				}
			}

		});

		if (user.role.equals("Administrator") == true) {
			grid = new Grid(6, 2);
			grid.setTitle("Usermanagment");
			grid.setText(0, 0, "Username");
			grid.setWidget(0, 1, lbUname);
			grid.setText(1, 0, "Username");
			grid.setWidget(1, 1, txtUname);
			grid.setText(2, 0, "Last Name");
			grid.setWidget(2, 1, txtLName);
			grid.setText(3, 0, "First Name");
			grid.setWidget(3, 1, txtFName);
			grid.setText(4, 0, "Password");
			grid.setWidget(4, 1, txtPwd1);
			grid.setText(5, 0, "Role");
			grid.setWidget(5, 1, lbRole);
			grid.setStyleName("center2");

			VerticalPanel vp_adm = new VerticalPanel();
			vp_adm.add(grid);
			grid = new Grid(5, 3);
			grid.setWidget(0, 0, btnUsrI);
			grid.setWidget(0, 1, btnUsrU);
			grid.setWidget(0, 2, btnUsrD);
			grid.setWidget(2, 1, btnDelRfc);
			grid.setWidget(4, 1, uploader);
			vp_adm.add(grid);
			vp_adm.add(serverResponseLabel);
			ScrollPanel sp = new ScrollPanel();
			sp.add(vp_adm);
			dp.add(sp, DockPanel.CENTER);
		}

		panel.selectTab(0);
		panel.setSize("800px", "600px");
		// panel.addStyleName("center2");
		RootPanel.get().add(panel);

		// Create a handler for the sendButton and nameField
		class MyHandler implements ClickHandler, KeyUpHandler {
			/**
			 * Fired when the user clicks on the sendButton.
			 */
			public void onClick(ClickEvent event) {

				int idxRole = lbRole.getSelectedIndex();
				// True if Create Button was used
				if (event.getSource() == btnUsrI) {

					dbService.userCreate(txtUname.getText(),
							txtLName.getText(), txtFName.getText(),
							lbRole.getItemText(idxRole), txtPwd1.getText(),
							(new AsyncCallback<String>() {

								@Override
								public void onFailure(Throwable caught) {
									// Show the RPC error message to the user
									dialogBox
											.setText("Remote Procedure Call - Failure");
									serverResponseLabel
											.addStyleName("serverResponseLabelError");
									serverResponseLabel.setHTML(SERVER_ERROR);
									dialogBox.center();
									closeButton.setFocus(true);
								}

								@Override
								public void onSuccess(String result) {
									// Show the RPC error message to the user

									dbService
											.getUsers(new AsyncCallback<String[]>() {
												public void onFailure(
														Throwable caught) {
													// Show the RPC error
													// message to the user
													dialogBox
															.setText("Remote Procedure Call - Failure");
													serverResponseLabel
															.addStyleName("serverResponseLabelError");
													serverResponseLabel
															.setHTML(SERVER_ERROR);
													dialogBox.center();
													closeButton.setFocus(true);
												}

												@Override
												public void onSuccess(
														String[] result) {

													dialogBox
															.setText("Success");
													dialogVPanel.clear();
													dialogVPanel.add(new HTML(
															"<b>User was successful created: "
																	+ txtFName
																			.getText()
																	+ "</b>"));
													dialogBox.center();
													dialogVPanel
															.add(closeButton);

													int i = 0;
													lbUname.clear();
													while (result[i] != null) {
														lbUname.addItem((String) result[i]);
														i++;
													}
												}
											});
								}
							}));
				}

				// True if Update Button was used
				if (event.getSource() == btnUsrU) {

					dbService.userChange(txtUname.getText(),
							txtFName.getText(), txtLName.getText(),
							lbRole.getItemText(lbRole.getSelectedIndex()),
							txtPwd1.getText(), (new AsyncCallback<String>() {

								@Override
								public void onFailure(Throwable caught) {
									dialogBox
											.setText("Remote Procedure Call - Failure");
									serverResponseLabel
											.addStyleName("serverResponseLabelError");
									serverResponseLabel.setHTML(SERVER_ERROR);
									dialogBox.center();
									closeButton.setFocus(true);

								}

								@Override
								public void onSuccess(String result) {
									dialogBox.setText("Info");
									dialogVPanel.clear();
									dialogVPanel.add(new HTML("<b>" + result
											+ "</b>"));
									dialogBox.center();
									dialogVPanel.add(closeButton);

								}
							}));
				}

				// True if Delete Button was used
				if (event.getSource() == btnUsrD) {

					dbService.userDelete(txtUname.getText(),
							(new AsyncCallback<String>() {

								@Override
								public void onFailure(Throwable caught) {
									dialogBox
											.setText("Remote Procedure Call - Failure");
									serverResponseLabel
											.addStyleName("serverResponseLabelError");
									serverResponseLabel.setHTML(SERVER_ERROR);
									dialogBox.center();
									closeButton.setFocus(true);

								}

								@Override
								public void onSuccess(String result) {
									dialogBox.setText("Success");
									dialogVPanel.clear();
									dialogVPanel
											.add(new HTML(
													"<b>User was successful deleted</b>"));
									dialogBox.center();
									dialogVPanel.add(closeButton);

								}
							}));

				}

				// True if Create Button was used
				if (event.getSource() == btnRFCCreate) {

					// Create the new RFC and add it to the list
					RfcList.add(new Rfc(
							Integer.parseInt(txtIDRFC.getText()),
							txtReasonRFC.getText(),
							txtDescRFC.getText(),
							txtDetailDescRFC.getText(),
							StartRFC.getValue(),
							null,
							null,
							null,
							null,
							null,
							txtCI.getText(),
							lbCO.getItemText(lbCO.getSelectedIndex()),
							lbCA.getItemText(lbCA.getSelectedIndex()),
							"",
							lbPrio.getItemText(lbPrio.getSelectedIndex()),
							txtCostsRFC.getText(),
							txtBenefitRFC.getText(),
							lbService.getItemText(lbService.getSelectedIndex()),
							txtRisksRFC.getText(), lbCI.getItemText(lbCI
									.getSelectedIndex()), "CREATED"));

					// Update ListView
					rfcTab.setRowCount(RfcList.size(), true);
					rfcTab.setRowData(0, RfcList);

					// Write new RFC in the Datastore
					dbService
							.createRFC(txtIDRFC.getValue(), txtReasonRFC
									.getText(), txtDescRFC.getText(),
									txtDetailDescRFC.getText(), StartRFC
											.getValue(), txtCI.getText(), lbCO
											.getItemText(lbCO
													.getSelectedIndex()), lbCA
											.getItemText(lbCA
													.getSelectedIndex()),
									lbPrio.getItemText(lbPrio
											.getSelectedIndex()), txtCostsRFC
											.getText(),
									txtBenefitRFC.getText(), txtRisksRFC
											.getText(), lbService
											.getItemText(lbService
													.getSelectedIndex()), lbCI
											.getItemText(lbCI
													.getSelectedIndex()),
									(new AsyncCallback<String>() {

										@Override
										public void onFailure(Throwable caught) {
											// Show the RPC error message to the
											// user
											dialogBox
													.setText("Remote Procedure Call - Failure");
											serverResponseLabel
													.addStyleName("serverResponseLabelError");
											serverResponseLabel
													.setHTML(SERVER_ERROR);
											dialogBox.center();
											closeButton.setFocus(true);
										}

										@Override
										public void onSuccess(String result) {

											// Show the RPC error message to the
											// user

											dialogBox.setText("Success");
											dialogVPanel.clear();
											dialogVPanel.add(new HTML("<b>"
													+ result + "</b>"));
											dialogVPanel.add(closeButton);
											dialogBox.center();
											closeButton.setFocus(true);

										}

									}));

					// new RfcNr
					txtIDRFC.setValue(txtIDRFC.getValue() + 1);

					dbService.getNextRfcNr(new AsyncCallback<Long>() {
						public void onFailure(Throwable caught) {
							// Show the RPC error message to the user
						}

						// Refresh Rfc Nr -> Async Check for newer Nr
						public void onSuccess(Long RfcNr) {
							if (RfcNr > txtIDRFC.getValue())
								txtIDRFC.setValue(RfcNr);
						}
					});

				}

				// True if Delete RFC was used
				if (event.getSource() == btnDelRfc) {
					dbService.deleteAllRfcs(new AsyncCallback<String>() {
						public void onFailure(Throwable caught) {

							dialogBox.setText("Failure");
							dialogVPanel.clear();
							dialogVPanel.add(new HTML("<b> Delete Error</b>"));
							dialogVPanel.add(closeButton);
							dialogBox.center();
							closeButton.setFocus(true);

						}

						@Override
						public void onSuccess(String result) {

							dialogBox.setText("Success");
							dialogVPanel.clear();
							dialogVPanel.add(new HTML("<b>" + result + "</b>"));
							dialogVPanel.add(closeButton);
							dialogBox.center();
							closeButton.setFocus(true);

							txtIDRFC.setValue((long) 1);

							RfcList.clear();
							rfcTab.setRowCount(RfcList.size(), true);
							rfcTab.setRowData(0, RfcList);

							WOList.clear();
							woTab.setRowCount(WOList.size(), true);
							woTab.setRowData(0, WOList);

						}
					});
				}

				// True if Delete RFC was used
				if (event.getSource() == btnSubmit) {
					// uploadForm.submit();
				}
				// True if Export RFC was used
				if (event.getSource() == btnRFCExport) {

					final Button btnExpClose = new Button("Close");
					//ScrollPanel sp = new ScrollPanel();
					//sp.setSize("420px", "400px");

					TextArea export = new TextArea();
					export.setText(exportRfcs(RfcList));

					export.setSize("420px", "400px");
					VerticalPanel vp = new VerticalPanel();
					vp.add(btnExpClose);
					vp.add(export);

					final DialogBox exportBox = new DialogBox();
					exportBox.setText("RFC Export View");

					//sp.setWidget(vp);
					exportBox.setWidget(vp);
					exportBox.center();

					// Add a handler CLOSE
					btnExpClose.addClickHandler(new ClickHandler() {
						public void onClick(ClickEvent event) {
							exportBox.hide();
						}
					});

				}
			}

			/**
			 * Fired when the user types in the nameField.
			 */
			public void onKeyUp(KeyUpEvent event) {
				if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {

				}
			}

			
			public String exportRfcs(List<Rfc> rfcs) {
				
				String xml = "<?xml version='1.0' ?>" +"\n";
				Iterator<Rfc> it = rfcs.listIterator();
				while(it.hasNext()){
					Rfc act = it.next();
					xml += "<rfc>" +"\n";
							xml += "<id>" +act.getIdStr() + "</id>" +"\n";
							xml += "<reason>" +act.getReason() +"</reason>" +"\n";
							xml += "<desc>" + act.getDesc()   + "</desc>" +"\n";
							xml += "<detaildesc>" + act.getDetailDesc()   + "</detaildesc>" +"\n";
							xml += "<validfrom>"+act.getValidFrom().toString() + "</validfrom>" +"\n";
							xml += "<ci>" +act.getCI()    + "</ci>" +"\n";
							xml += "<co>"+act.getCO()  + "</co>" +"\n";
							xml += "<ca>"+act.getCA()  + "</ca>" +"\n";
							xml += "<prio>"+act.getPrio()  + "</prio>" +"\n";
							xml += "<cost>"+act.getCost() + "</cost>" +"\n";
							xml += "<benefit>"+act.getBenefit() + "</benefit>" +"\n";
							xml += "<risk>"+act.getRisk() + "</risk>" +"\n";
							xml += "<service>"+act.getServ() + "</service>" +"\n" ;
							xml += "<citem>"+act.getcItem() + "</citem>" +"\n";
						xml += "</rfc>"+"\n"+"\n";
				}
				
				return xml;
				
//				<?xml version="1.0" ?>
//				<rfc>
//				<id>1</id>
//				<reason>Support not valid</reason>
//				<desc>The current version ist out of support</desc>
//				<validfrom>06-Jul-2012 12:00:00</validfrom>
//				<ci>CI_1</ci>
//				<co>CO_1</co>
//				<ca>CA_1</ca>
//				<prio>Normal</prio>
//				<cost>1200</cost>
//				<benefit>Support garanted</benefit>
//				<risk>very low</risk>
//				<service>Service A</service>
//				<citem>Software</citem>
//				</rfc>
//				ByteArrayOutputStream baos = new ByteArrayOutputStream();
//				try {
//					XMLEncoder encoder = new XMLEncoder(baos);
//					encoder.writeObject(rfcs);
//					encoder.close();
//				} catch (Exception e) {
//					e.printStackTrace();
//				}
			
			}
			
			
		}

		// Add a handler to send the name to the server
		MyHandler handler = new MyHandler();
		btnUsrI.addClickHandler(handler);
		btnUsrU.addClickHandler(handler);
		btnUsrD.addClickHandler(handler);
		btnDelRfc.addClickHandler(handler);
		btnRFCCreate.addClickHandler(handler);
		btnRFCExport.addClickHandler(handler);

		// Inital User Load
		dbService.getUsers(new AsyncCallback<String[]>() {
			public void onFailure(Throwable caught) {
				// Show the RPC error message to the user
				dialogBox.setText("Remote Procedure Call - Failure");
				serverResponseLabel.addStyleName("serverResponseLabelError");
				serverResponseLabel.setHTML(SERVER_ERROR);
				dialogBox.center();
				closeButton.setFocus(true);
			}

			@Override
			public void onSuccess(String[] result) {
				int i = 0;
				while (result[i] != null) {
					lbUname.addItem((String) result[i]);
					i++;
				}
			}
		});

		// Listbox

		dbService.getCA(new AsyncCallback<String[]>() {
			public void onFailure(Throwable caught) {
				// Show the RPC error message to the user
				dialogBox.setText("Remote Procedure Call - Failure");
				serverResponseLabel.addStyleName("serverResponseLabelError");
				serverResponseLabel.setHTML(SERVER_ERROR);
				dialogBox.center();
				closeButton.setFocus(true);
			}

			@Override
			public void onSuccess(String[] result) {
				int i = 0;
				while (result[i] != null) {
					lbCA.addItem((String) result[i]);
					i++;
				}
			}
		});

		dbService.getCO(new AsyncCallback<String[]>() {
			public void onFailure(Throwable caught) {
				// Show the RPC error message to the user
				dialogBox.setText("Remote Procedure Call - Failure");
				serverResponseLabel.addStyleName("serverResponseLabelError");
				serverResponseLabel.setHTML(SERVER_ERROR);
				dialogBox.center();
				closeButton.setFocus(true);
			}

			@Override
			public void onSuccess(String[] result) {
				int i = 0;
				while (result[i] != null) {
					lbCO.addItem((String) result[i]);
					i++;
				}
			}
		});

		
		

		
		
	}

	// Get Today Date
	public static Date getToday() {
		Date today = new Date();
		return today;
	}



	// Load the image in the document and in the case of success attach it to
	// the viewer
	IUploader.OnFinishUploaderHandler onFinishUploaderHandler = new IUploader.OnFinishUploaderHandler() {
		public void onFinish(IUploader uploader) {
			if (uploader.getStatus() == Status.SUCCESS) {

				// The server sends useful information to the client by default
				UploadedInfo info = uploader.getServerInfo();
				System.out.println("File name " + info.name);
				System.out.println("File content-type " + info.ctype);
				System.out.println("File size " + info.size);

				// You can send any customized message and parse it
				System.out.println("Server message " + info.message);

			}
		}
	};

}
